import { ALL_CATEGORY_ITEM } from '@/constants'
import { isMobileTerminal } from '@/utils/flexible'
import { parse, stringify } from 'zipson'
import { defineStore } from 'pinia'

export const useAppStore = defineStore('appStore', {
  state: () => ({
    // 当前选中的分类
    currentCategory: ALL_CATEGORY_ITEM,
    // 搜索的文本
    searchText: '',
    // 路由跳转类型
    routerType: 'none',
  }),
  getters: {
    /**
     * category选中项
     */
    currentCategory: (state) => state.currentCategory,
    /**
     * 搜索文本
     */
    searchText: (state) => state.searchText,
    // 路由跳转方式
    routerType: (state) => {
      // 在 PC 端下，永远为 none
      if (!isMobileTerminal.value) {
        return 'none'
      }
      return state.routerType
    },
  },
  actions: {
    /**
     * 切换选中分类
     */
    changeCurrentCategory(newCategory: any) {
      this.currentCategory = newCategory
    },
    /**
     * 修改 searchText
     */
    changeSearchText(newSearchText: string) {
      this.searchText = newSearchText
    },
    /**
     * 修改 routerType
     */
    changeRouterType(newType: string) {
      this.routerType = newType
    },
  },
  persist: {
    key: 'appStore',
    storage: localStorage,
     serializer: {
      deserialize: parse,
      serialize: stringify
    }
  },
})
